package com.cmc6.user.controller;

import com.cmc6.common.exception.BaseBizCodeEnum;
import com.cmc6.common.model.dto.NotBlankId;
import com.cmc6.common.util.ApiResult;
import com.cmc6.user.model.dto.EmailNotBlankDTO;
import com.cmc6.user.model.dto.ForgotPasswordByPhoneDTO;
import com.cmc6.user.model.dto.PhoneNotBlankDTO;
import com.cmc6.user.model.dto.UpdatePasswordByCodeDTO;
import com.cmc6.user.service.ForgotPasswordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "忘记密码控制器")
@RestController
@RequestMapping(value = "/forgotPassword")
public class ForgotPasswordController {

    @Resource
    ForgotPasswordService baseService;

    @PostMapping
    @ApiOperation(value = "忘记密码-发送链接")
    public ApiResult<String> forgotPassword(@RequestBody @Valid EmailNotBlankDTO dto) {
        return ApiResult.ok(baseService.forgotPassword(dto));
    }

    @PostMapping(value = "/getCodeByOpen")
    @ApiOperation(value = "通过 open码获取 code")
    public ApiResult<String> getCodeByOpen(@RequestBody @Valid NotBlankId notBlankId) {
        return ApiResult.ok(BaseBizCodeEnum.API_RESULT_OK.getMsg(), baseService.getCodeByOpen(notBlankId));
    }

    @PostMapping(value = "/updatePasswordByCode")
    @ApiOperation(value = "通过 code修改密码")
    public ApiResult<String> updatePasswordByCode(@RequestBody @Valid UpdatePasswordByCodeDTO dto) {
        return ApiResult.ok(baseService.updatePasswordByCode(dto));
    }

    @PostMapping(value = "/phone/sendCode")
    @ApiOperation(value = "忘记密码，给手机发送验证码")
    public ApiResult<String> forgotPasswordByPhoneSendCode(@RequestBody @Valid PhoneNotBlankDTO dto) {
        return ApiResult.ok(baseService.forgotPasswordByPhoneSendCode(dto));
    }

    @PostMapping(value = "/phone")
    @ApiOperation(value = "重置密码：通过手机验证码")
    public ApiResult<String> forgotPasswordByPhone(@RequestBody @Valid ForgotPasswordByPhoneDTO dto) {
        return ApiResult.ok(baseService.forgotPasswordByPhone(dto));
    }

}
